<?php
// SoundCloud shortcode
	function shortcode_soundcloud($atts) {
		$atts = shortcode_atts(
			array(
				'url' => '',
				'width' => '100%',
				'height' => 81,
				'comments' => 'true',
				'auto_play' => 'true',
				'color' => 'ff7700',
			), $atts);
		
			return '<object height="' . $atts['height'] . '" width="' . $atts['width'] . '"><param name="movie" value="http://player.soundcloud.com/player.swf?url=' . urlencode($atts['url']) . '&amp;show_comments=' . $atts['comments'] . '&amp;auto_play=' . $atts['auto_play'] . '&amp;color=' . $atts['color'] . '"></param><param name="allowscriptaccess" value="always"></param><embed allowscriptaccess="always" height="' . $atts['height'] . '" src="http://player.soundcloud.com/player.swf?url=' . urlencode($atts['url']) . '&amp;show_comments=' . $atts['comments'] . '&amp;auto_play=' . $atts['auto_play'] . '&amp;color=' . $atts['color'] . '" type="application/x-shockwave-flash" width="' . $atts['width'] . '"></embed></object>';
	}

// Youtube shortcode
	function shortcode_youtube($atts) {
		$atts = shortcode_atts(
			array(
				'id' => '',
				'width' => 620,
				'height' => 360
			), $atts);
		
			return '<div class="video-shortcode embed-container "><iframe title="YouTube video player" width="' . $atts['width'] . '" height="' . $atts['height'] . '" src="http://www.youtube.com/embed/' . $atts['id'] . '" frameborder="0" allowfullscreen></iframe></div>';
	}
	

// Vimeo shortcode
	function shortcode_vimeo($atts) {
		$atts = shortcode_atts(
			array(
				'id' => '',
				'width' => 620,
				'height' => 360
			), $atts);
		
			return '<div class="video-shortcode embed-container "><iframe src="http://player.vimeo.com/video/' . $atts['id'] . '" width="' . $atts['width'] . '" height="' . $atts['height'] . '" frameborder="0"></iframe></div>';
	}
	

	
// Button shortcode
	function shortcode_button($atts, $content = null) {
		$atts = shortcode_atts(
			array(
				'background' => '#F96E5B',
				'color' => '#f8f8f8',
				'size' => 'medium',
				'link' => '#',
				'target' => ''
			), $atts);
		
			return '<span class="button ' . $atts['size'] . '" style="background:' . $atts['background'] . ';"><a style="color:' . $atts['color'] . ';" href="' . $atts['link'] . '" target="' . $atts['target'] . '">' .do_shortcode($content). '</a></span>';
	}
	

// Dropcap shortcode
	function shortcode_dropcap( $atts, $content = null ) {  
		
		return '<span class="dropcap">' .do_shortcode($content). '</span>';  
		
}
	

// Highlight shortcode
	function shortcode_highlight($atts, $content = null) {
		$atts = shortcode_atts(
			array(
				'highlight-color' => '#F96E5B',
				'font-color' => '#f8f8f8'
			), $atts);
				
			return '<span style="background:' . $atts['highlight-color'] . '; color:' . $atts['font-color'] . ';">' .do_shortcode($content). '</span>';
	}
	


// Check list shortcode
	function shortcode_checklist( $atts, $content = null ) {
	
	$content = str_replace('<ul>', '<ul class="checklist">', do_shortcode($content));
	$content = str_replace('<li>', '<li>', do_shortcode($content));
	
	return $content;
	
}

// Bad list shortcode
	function shortcode_badlist( $atts, $content = null ) {
	
	$content = str_replace('<ul>', '<ul class="badlist">', do_shortcode($content));
	$content = str_replace('<li>', '<li>', do_shortcode($content));
	
	return $content;
	
}

// Block list shortcode
	function shortcode_blocklist( $atts, $content = null ) {
	
	$content = str_replace('<ul>', '<ul class="blocklist">', do_shortcode(
	$content));
	$content = str_replace('<li>', '<li>', do_shortcode($content));
	
	return $content;
	
}

// Tabs shortcode
	function shortcode_tabs( $atts, $content = null ) {
	extract(shortcode_atts(array(
    ), $atts));
	$out .= '<div class="tabs-wrapper">';
	
	$out .= '<ul class="tabs">';

	foreach ($atts as $key => $tab) {
	$out .= '<li><a href="#' . $key . '">' . $tab . '</a></li>';
	}
	$out .= '</ul>';
	$out .= '<div class="clear"></div>';
	$out .= '<div class="tabs-container">';

	$out .= do_shortcode($content); 
	$out .='</div></div>';
	return $out;
}


	function shortcode_tab( $atts, $content = null ) {
	extract(shortcode_atts(array(
    ), $atts));
	$out .= '<div id="tab' . $atts['id'] . '" class="tab_content">';
	$out .= do_shortcode($content);
	$out .= '</div>';
	
	return $out;
}

// Toggle shortcode
	function shortcode_toggle( $atts, $content = null ) {
	extract(shortcode_atts(array(
        'title'      => '',
    ), $atts));
	
	$out .= '<h5 class="toggle"><a href="#">' .$title. '</a></h5>';
	$out .= '<div class="toggle-content">';
	$out .= '<div class="block">';
	$out .= do_shortcode($content);
	$out .= '</div>';
	$out .= '</div>';
	
   return $out;
}

// Column one_half shortcode
	function shortcode_one_half($atts, $content = null) {
		 $atts = shortcode_atts(
			array(
				'last' => 'no',
			), $atts);
			
			if($atts['last'] == 'yes') {
				return '<div class="one_half last">' .do_shortcode($content). '</div><div class="clearboth"></div>';
			} else {
				return '<div class="one_half">' .do_shortcode($content). '</div>';
			}

	}


// Column one_third shortcode
	function shortcode_one_third($atts, $content = null) {
		$atts = shortcode_atts(
			array(
				'last' => 'no',
			), $atts);
			
			if($atts['last'] == 'yes') {
				return '<div class="one_third last">' .do_shortcode($content). '</div><div class="clearboth"></div>';
			} else {
				return '<div class="one_third">' .do_shortcode($content). '</div>';
			}

	}
	

// Column two_third shortcode
	function shortcode_two_third($atts, $content = null) {
		$atts = shortcode_atts(
			array(
				'last' => 'no',
			), $atts);
			
			if($atts['last'] == 'yes') {
				return '<div class="two_third last">' .do_shortcode($content). '</div><div class="clearboth"></div>';
			} else {
				return '<div class="two_third">' .do_shortcode($content). '</div>';
			}

	}

// Column one_fourth shortcode
	function shortcode_one_fourth($atts, $content = null) {
		$atts = shortcode_atts(
			array(
				'last' => 'no',
			), $atts);
			
			if($atts['last'] == 'yes') {
				return '<div class="one_fourth last">' .do_shortcode($content). '</div><div class="clearboth"></div>';
			} else {
				return '<div class="one_fourth">' .do_shortcode($content). '</div>';
			}

	}


	
// Column three_fourth shortcode
	function shortcode_three_fourth($atts, $content = null) {
		$atts = shortcode_atts(
			array(
				'last' => 'no',
			), $atts);
			
			if($atts['last'] == 'yes') {
				return '<div class="three_fourth last">' .do_shortcode($content). '</div><div class="clearboth"></div>';
			} else {
				return '<div class="three_fourth">' .do_shortcode($content). '</div>';
			}

	}

// Attention shortcode
	function shortcode_attention( $atts, $content = null ) {  
		
		return '<div class="attention"><p><span>Attention!  </span>' .do_shortcode($content). '</p></div>';  
		
}

// Information shortcode
	function shortcode_information( $atts, $content = null ) {  
		
		return '<div class="information"><p><span>Information!  </span>' .do_shortcode($content). '</p></div>';  
		
}

// Error shortcode
	function shortcode_error( $atts, $content = null ) {  
		
		return '<div class="error_warning"><p><span>Error!  </span>' .do_shortcode($content). '</p></div>';  
		
}


// Google map shortcode
	function shortcode_googleMaps($atts, $content = null) {
	extract(shortcode_atts(array(
		"width" => '630',
		"height" => '480',
		"src" => ''
	), $atts));
	return '<iframe width="'.$width.'" height="'.$height.'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$src.'&amp;output=embed"></iframe>';
	
	}

// Prevent the WPAutoP Filter
function pre_process_shortcode($content) {
    global $shortcode_tags;
    // Backup current registered shortcodes and clear them all out
    $orig_shortcode_tags = $shortcode_tags;
    $shortcode_tags = array();
		add_shortcode('soundcloud', 'shortcode_soundcloud');
		add_shortcode('youtube', 'shortcode_youtube');
		add_shortcode('vimeo', 'shortcode_vimeo');
		add_shortcode('button', 'shortcode_button');
		add_shortcode('dropcap', 'shortcode_dropcap');
		add_shortcode('highlight', 'shortcode_highlight');
		add_shortcode('checklist', 'shortcode_checklist');
		add_shortcode('badlist', 'shortcode_badlist');
		add_shortcode('blocklist', 'shortcode_blocklist');
		add_shortcode('tabs', 'shortcode_tabs');
		add_shortcode('tab', 'shortcode_tab');
		add_shortcode('toggle', 'shortcode_toggle');
		add_shortcode('one_fourth', 'shortcode_one_fourth');
        add_shortcode("one_half","shortcode_one_half");
		add_shortcode('one_third', 'shortcode_one_third');
		add_shortcode('two_third', 'shortcode_two_third');
		add_shortcode('three_fourth', 'shortcode_three_fourth');
		add_shortcode('attention', 'shortcode_attention');
		add_shortcode('information', 'shortcode_information');
		add_shortcode('error', 'shortcode_error');
		add_shortcode("googlemap", "shortcode_googleMaps");
    // Do the shortcode (only the one above is registered)
    $content = do_shortcode($content);
    // Put the original shortcodes back
    $shortcode_tags = $orig_shortcode_tags;
    return $content;
}
add_filter('the_content', 'pre_process_shortcode', 7);
add_filter('widget_text', 'pre_process_shortcode');


// Add buttons to tinyMCE
add_action('init', 'add_button');

function add_button() {  
   if ( current_user_can('edit_posts') &&  current_user_can('edit_pages') )  
   {  
     add_filter('mce_external_plugins', 'add_plugin');  
     add_filter('mce_buttons_3', 'register_button');  
   }  
}  

function register_button($buttons) {  
   array_push($buttons, "soundcloud", "youtube", "vimeo","googlemap", "button", "dropcap", "highlight", "tabs", "toggle", "checklist", "badlist", "blocklist", "attention", "information","error", "one_half", "one_third", "two_third", "one_fourth", "three_fourth" );  
   return $buttons;  
}  

function add_plugin($plugin_array) {  
   $plugin_array['soundcloud'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['youtube'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['vimeo'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['googlemap'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['button'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['dropcap'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['highlight'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['tabs'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['toggle'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['checklist'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['badlist'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['blocklist'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['attention'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['information'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['error'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['one_half'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['one_third'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['two_third'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['one_fourth'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
   $plugin_array['three_fourth'] = get_template_directory_uri().'/functions/shortcodes/customcodes.js';
 
   return $plugin_array;  
}  